package com.example.yimai.utils;

import org.elasticsearch.index.query.BoolQueryBuilder;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.query.FetchSourceFilter;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;

public class ESUtil {


    public static NativeSearchQuery buildQuery(int pageNum, int pageSize, BoolQueryBuilder queryParams) {
        Pageable pageable = PageRequest.of(pageNum, pageSize);

        // 创建查询条件
        NativeSearchQuery query = new NativeSearchQueryBuilder()
                .withQuery(queryParams)
                .withPageable(pageable)
                .build();
        query.addSourceFilter(new FetchSourceFilter(new String[]{"*"},new String[]{}));
        return query;
    }









//    绕进去了，不知道在写什么 汗颜.jpg
//    private final ElasticsearchClient client;
//
//    public ESUtil(){
//        //创建低级RestClient
//        RestClient restClient = RestClient.builder(
//                new HttpHost("localhost", 9201)
//        ).build();
//        //创建传输层
//        ElasticsearchTransport transport = new RestClientTransport(restClient,new JacksonJsonpMapper());
//        //创建ElasticsearchClient
//        this.client = new ElasticsearchClient(transport);
//    }
//
//
//    public <T> String add(String index,T entity){
//        try{
//            client.index(i -> i.index(index).document(entity));
//            return "success";
//        }catch (Exception e){
//            e.printStackTrace();
//            throw new RuntimeException("添加失败" + e.getMessage());
//        }
//    }

}
